// pages/sale/orderDetail/orderDetail.js
var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var user = require('../../../utils/user.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderId: 0,
    orderInfo: {},
    orderGoods: [],
    handleOption: {},
    showType:1, //订单列表的头部tab页的类型
    startOrderNUm:'',//运货起始单号
    endOrderNUm:'',//运货终止单号
    index: 0,//订单状态类型
    startcode: null, //起始单号的值
    endcode:null //终止单号的值
  },
  //获取订单
  getOrderDetail: function () {
    let that = this;
    util.request(api.OrderDetail, {
      orderId: that.data.orderId
    }).then(function (res) {
      if (res.errno === 0) {
        console.log(res.data);
        that.setData({
          orderInfo: res.data.orderInfo,
          orderGoods: res.data.orderGoods,
          endTime: new Date().getTime() + res.data.orderInfo.toMillis
          // handleOption: res.data.orderInfo.handleOption
        });
        util.timecutDown(that)
        // 请求物流信息,仅当订单状态为发货时才请求
        // if (res.data.orderInfo.handleOption.confirm) {
        //   that.getOrderExpress();
        // }
      }
    });
  },
  //确认订单
  comfirmOrder:function(e){
    console.log(e)
    console.log("id:"+e.currentTarget.dataset.id)
    util.request(api.OrderConfirm, { id: e.currentTarget.dataset.id,orderStatus: 101},"POST").then(function(res){
      if(res.errno===0){
          wx.showToast({
            success:'success',
            title: '已确认订单',
          })
        setTimeout(function () {
          wx.navigateBack({
            delta: 1
          })
        }, 2000)
      }
    });
  },
  //确认收款
  comfirmReceiv: function (e) {
    console.log(e)
    console.log("id:" + e.currentTarget.dataset.id)
    util.request(api.OrderConfirm, { id: e.currentTarget.dataset.id, orderStatus: 102 }, "POST").then(function (res) {
      if (res.errno === 0) {
        wx.showToast({
          success: 'success',
          title: '已确认收款',
        })
        setTimeout(function(){
          wx.navigateBack({
            delta: 1
          })
        },2000)
        
      }
    });
  },
  //获取起始单号
  startOrderNUm:function(e){
    console.log('起始单号')
    console.log(e.detail.value)
    this.setData({
      startOrderNUm: e.detail.value
    })
  },
  //获取终止单号
  endOrderNUm: function (e) {
    console.log('终止单号')
    console.log(e.detail.value)
    this.setData({
      endOrderNUm: e.detail.value
    })
  },
  //下一步
  nextStep:function(e){
    wx.setStorageSync('orderGoods', this.data.orderGoods);
    wx.setStorageSync('orderInfo', this.data.orderInfo)
    wx.navigateTo({
      url: '/pages/sale/sendGoods/sendGoods',
    })
  },
  // 扫二维码
  scanCode:function(e){
    console.log('扫码结果')
    console.log(e.currentTarget.dataset.index)
    let index = e.currentTarget.dataset.index
    let that = this
    wx.scanCode({
      success(res) {
        console.log(res)
       if(index=='start'){
         that.setData({
           startcode: res.result
         })
       }else{
         that.setData({
           endcode: res.result
         })
       }
      },
      fail() {
        util.showErrorToast('扫码失败')
      }
    })
  },
  //获取卖家备注
  getSellRemarks:function(e){
    console.log('卖家备注'+e.detail.value)
    wx.setStorageSync('sellRemarks', e.detail.value)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    console.log("订单id：" + options.id)
    console.log("showType：" + options.showType)
    console.log("index：" + options.index)
    this.setData({
      orderId: options.id,
      showType: options.showType,
      index: options.index,
    });
    this.getOrderDetail();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.removeStorageSync('sellRemarks')
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})